﻿using Ornament.Web.Mvc;
using System;
using Products.DataAcccess;
using Products.Databases;

namespace Ornament.MVCWebFrame.Models
{
    public class TableBinder : BaseModelBinder
    {
        protected override object RequestId(System.Web.HttpRequestBase request)
        {
            if (request["Id"] == null)
                return null;
            return new Guid(request["Id"]);
        }
        protected override object CreateNewObject(System.Web.Mvc.ControllerContext controllerContext,
            System.Web.Mvc.ModelBindingContext bindingContext, System.Type modelType)
        {
            var request = controllerContext.RequestContext.HttpContext.Request;
            if (request["projectId"] == null)
                throw new ArgumentOutOfRangeException("Post data need projectId");
            if (request["versionId"] == null)
            {
                throw new ArgumentOutOfRangeException("Post data need versionId");
            }
            var projectId = new Guid(request["projectId"]);
            var project = (new ProductDao()).Get(projectId);
            var version = (new VersionDao()).Get(new Guid(request["versionId"]));
            return new Table(project, version);

        }
    }
}